package View;
import java.awt.*;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;// to allow event handling purpose

import javax.swing.*; 
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Controller.AddressBookController;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;


/**
 * The main view of the AdressBook
 * list of contacts and groups with buttons to add,modifie,get details, sort
 */	
	
public class AddressBookView extends JFrame {

	private AddressBookView that = this;
	
	private static final long serialVersionUID = 1L;

	AddressBookController controller;
	
	private JButton bCreate,bModifie,bDelete,bDetails,bSortName,bSortGroup,bSortDate,bSave,bLoad,bSearch;
	private JTextField tfSearch;
	private JList<String> list,groupList;
    private DefaultListModel<String> listModel,groupListModel;
	
    private JPanel main;
	private JPanel p1,p2,p3,p4;
	private Color peach= new Color(255, 229, 180);
	
	
	public static String CREATE = "Create";
	public static String MODIFIE = "Modifie";
	public static String DELETE ="Delete";
	public static String DETAILS = "Details";
	public static String SAVE = "Save";
	public static String LOAD ="Load";
	public static String SEARCH ="Search";
	public static String SORTBYNAME = "Sort by Name";
	public static String SORTBYGROUP = "Sort by Group";
	public static String SORTBYDATE = "Sort by Date";
	public static String BACKGROUND_FILE = "background.jpg";
	
	//Default Constructor
	
	public AddressBookView() {
		
		controller= new AddressBookController(that);
		
		//P1 holds the buttons to act on the contacts
		p1 = new JPanel();
		p1.setLayout(new FlowLayout());
		bCreate = new JButton(CREATE);
		bCreate.setBackground(peach);
		bCreate.setOpaque(false);
		bModifie = new JButton(MODIFIE);
		bModifie.setBackground(peach);
		bModifie.setOpaque(false);
		bDelete = new JButton(DELETE);
		bDelete.setBackground(peach);
		bDelete.setOpaque(false);
		bDetails = new JButton(DETAILS);
		bDetails.setBackground(peach);
		bDetails.setOpaque(false);
		bSave = new JButton(SAVE);
		bSave.setBackground(peach);
		bSave.setOpaque(false);
		bLoad = new JButton(LOAD);
		bLoad.setBackground(peach);
		bLoad.setOpaque(false);
		p1.add(bCreate);
		p1.add(bModifie);
		p1.add(bDelete);
		p1.add(bDetails);
		p1.add(bSave);
		p1.add(bLoad);
		
		//P2 holds the sort buttons
		p2 = new JPanel();
		p2.setLayout(new FlowLayout());
		bSearch = new JButton(SEARCH);
		bSearch.setBackground(peach);
		bSearch.setOpaque(false);
		bSearch.setOpaque(false);
		tfSearch = new JTextField(10);
		tfSearch.setBackground(peach);
		tfSearch.setOpaque(false);
		bSortName=new JButton(SORTBYNAME);
		bSortName.setBackground(peach);
		bSortName.setOpaque(false);
		bSortGroup=new JButton(SORTBYGROUP);
		bSortGroup.setBackground(peach);
		bSortGroup.setOpaque(false);
		bSortDate=new JButton(SORTBYDATE);
		bSortDate.setBackground(peach);
		bSortDate.setOpaque(false);
		
		p2.add(bSearch);
		p2.add(tfSearch);
		p2.add(bSortName);
		p2.add(bSortGroup);
		p2.add(bSortDate);
		
		//P3 holds the Jlist
		p3 = new JPanel();
		
		//Create a list and put it in a Scoll
        listModel = new DefaultListModel<String>();
		list = new JList<String>(listModel);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setSelectedIndex(0);
        list.setVisibleRowCount(20);
        list.setSelectionForeground(Color.RED);
        list.setOpaque(false);
        ((DefaultListCellRenderer) this.list.getCellRenderer()).setOpaque(false);
        JScrollPane listScrollPane = new JScrollPane(list);
        listScrollPane.setOpaque(false);
        listScrollPane.getViewport().setOpaque(false);
        p3.add(listScrollPane);
        
        //P4 holds the groupList
        p4 = new JPanel();
        
        groupListModel = new DefaultListModel<String>();
		
		groupList = new JList<String>(groupListModel);
		groupList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		groupList.setSelectedIndex(0);
		groupList.setVisibleRowCount(20);
		groupList.setSelectionForeground(Color.RED);
		groupList.setOpaque(false);
		((DefaultListCellRenderer) this.groupList.getCellRenderer()).setOpaque(false);
		groupList.setBackground(peach);
        JScrollPane listScrollPane2 = new JScrollPane(groupList);
        listScrollPane2.setOpaque(false);
        listScrollPane2.getViewport().setOpaque(false);
        p4.add(listScrollPane2);
		
        
        //main holds all the Panels
        final ImageIcon icon = new ImageIcon(BACKGROUND_FILE);
        
        main = new JPanel(new BorderLayout()){
    			/**
    			 * 
    			 */
    			private static final long serialVersionUID = 1L;

    			protected void paintComponent(Graphics g)
    			{
    				//  Display image
    				g.drawImage(icon.getImage(), 0, 0, null); 	
    				super.paintComponent(g);
    			}
    		
        };
        main.setOpaque(false);
        p1.setOpaque(false);
        p2.setOpaque(false);
        p3.setOpaque(false);
        p4.setOpaque(false);
        
        main.add(p1, BorderLayout.SOUTH);
        main.add(p2, BorderLayout.NORTH);
        main.add(p3, BorderLayout.CENTER);
        main.add(p4, BorderLayout.WEST);
        
        
		//Add panels to the frame
		getContentPane().add(main);
		
		// Register listeners
		bCreate.addActionListener(controller);
		bModifie.addActionListener(controller);
		bDelete.addActionListener(controller);
		bDetails.addActionListener(controller);
		bSortName.addActionListener(controller);
		bSortGroup.addActionListener(controller);
		bSortDate.addActionListener(controller);
		bSearch.addActionListener(controller);
		bSave.addActionListener(controller);
		bLoad.addActionListener(controller);
        list.addListSelectionListener(controller);
        groupList.addListSelectionListener(controller);
        list.addMouseListener(controller);
        groupList.addMouseListener(controller);
        tfSearch.getDocument().addDocumentListener(controller);
        
        //Windows listener to do things when the window is closing or is opening
        addWindowListener(controller);
        
		
	}
	
	public String getSearch (){
		return tfSearch.getText();
	}
	
	public static void main (String []args) {
		
		
		AddressBookView frame = new AddressBookView();
		frame.setTitle("Address Book");
		frame.setSize(600,472);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	public String getSelectedValueGroupList(){
		return groupList.getSelectedValue();
	}
	
	public String getSelectedValueContactList(){
		return list.getSelectedValue();
	}
	
	public void setListModelGroup(DefaultListModel<String> listModel){
		groupList.setModel(listModel);
	}
	
	public void setListModelContacts(DefaultListModel<String> listModel){
		list.setModel(listModel);
	}
	
	public int getSelectedIndexContactList(){
		return list.getSelectedIndex();
	}
	
	public void setSelectedIndexContactList(int index){
		list.setSelectedIndex(index);
	}
	public int getSelectedIndexGroupList(){
		return groupList.getSelectedIndex();
	}
	
	public void setSelectedIndexGroupList(int index){
		groupList.setSelectedIndex(index);
	}
	public JList<String> getContactList(){
		return list;
	}
	public JList<String> getGoupList(){
		return groupList;
	}

}
